'use strict'
const exports = {}

// https://adndevblog.typepad.com/autocad/2017/09/dissecting-mtext-format-codes.html
// https://www.cadforum.cz/cadforum_en/text-formatting-codes-in-mtext-objects-tip8640
Object.defineProperty(exports, '__esModule', { value: true })
exports.parseDxfMTextContent = void 0
var dxfTextControlCodeSymbolMap = {
  d: '°',
  c: '⌀',
  p: '±'
}
var parseDxfMTextContent = function (s, options) {
  var _a, _b, _c, _d
  s = s.replace(/%%(.)/g, function (_, c) {
    return dxfTextControlCodeSymbolMap[c] || c
  })
  var encoding = options === null || options === void 0 ? void 0 : options.encoding
  var decoder = encoding instanceof TextDecoder ? encoding : undefined
  var currentText = ''
  var c
  var contents = []
  var pushContent = function (content) {
    if (currentText) {
      contents.push(currentText)
      currentText = ''
    }
    contents.push(content)
  }
  for (var i = 0; i < s.length; i++) {
    switch ((c = s[i])) {
      default:
        currentText += c
        break
      case '\\': {
        switch ((c = s[++i])) {
          default:
            currentText += c
            break
          case 'P':
            currentText += '\n'
            break
          case 'f':
          case 'F': {
            var f = ''
            while ((c = s[++i])) {
              if (c === ';') {
                pushContent({ f: f })
                break
              }
              if (c === '|') {
                var content = { f: f }
                var end = s.indexOf(';', ++i)
                for (var _i = 0, _e = s.slice(i, end).split('|'); _i < _e.length; _i++) {
                  var element = _e[_i]
                  content[element[0]] = +element.slice(1)
                }
                i = end
                pushContent(content)
                break
              }
              f += c === '\\' ? s[++i] : c
            }
            break
          }
          case 'S': {
            var upper = ''
            var op = void 0
            var lower = ''
            while ((c = s[++i])) {
              if (c === ';') {
                op && pushContent({ S: [upper, op, lower] })
                break
              }
              if (c === '\\') {
                op ? (lower += s[++i]) : (upper += s[++i])
              } else if (op) {
                lower += c
              } else if (c === '^' || c === '/' || c === '#') {
                op = c
              } else {
                upper += c
              }
            }
            break
          }
          case 'H':
          case 'W':
            var start = ++i
            var _f = s.slice(start, (i = s.indexOf(';', i))).match(/^(\d*(?:\.\d+)?)(\D*)$/),
              value = _f[1],
              unit = _f[2]
            pushContent(((_a = {}), (_a[c] = [+value, unit]), _a))
            break
          case 'Q':
          case 'A':
          case 'C':
          case 'T': {
            var start_1 = ++i
            pushContent(((_b = {}), (_b[c] = +s.slice(start_1, (i = s.indexOf(';', i)))), _b))
            break
          }
          case 'L':
          case 'O':
          case 'K':
            pushContent(((_c = {}), (_c[c] = 1), _c))
            break
          case 'l':
          case 'o':
          case 'k':
            pushContent(((_d = {}), (_d[c.toUpperCase()] = 0), _d))
            break
          case 'U':
          case 'u':
            if (s[i + 1] === '+') {
              currentText += String.fromCodePoint(parseInt(s.substr(i + 2, 4), 16))
              i += 5
            } else {
              currentText += c
            }
            break
          case 'M':
          case 'm':
            if (encoding) {
              if (s[i + 1] === '+' && s[i + 2] === '1') {
                currentText += (decoder = decoder || new TextDecoder(encoding)).decode(
                  new Uint8Array([
                    parseInt(s.substr(i + 3, 2), 16),
                    parseInt(s.substr(i + 5, 2), 16)
                  ])
                )
                i += 6
              } else {
                currentText += c
              }
            } else {
              currentText += '\\' + c
            }
            break
        }
        break
      }
      case '{': {
        var depth = 1
        var start = i
        while ((c = s[++i])) {
          if (c === '{') {
            depth++
          } else if (c === '}') {
            if (--depth === 0) {
              pushContent((0, exports.parseDxfMTextContent)(s.slice(start + 1, i)))
              break
            }
          } else if (c === '\\') {
            i++
          }
        }
        break
      }
    }
  }
  currentText && contents.push(currentText)
  return contents
}
exports.parseDxfMTextContent = parseDxfMTextContent

export { parseDxfMTextContent }
